<?php
error_reporting(E_ALL);
date_default_timezone_set('Asia/Ho_Chi_Minh');
require_once 'define.php';
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalconvn\Auth\Auth;
use Phalconvn\Email\Send;
use Phalconvn\Acl\Acl;
use Phalconvn\Views\Seo;
class Application extends \Phalcon\Mvc\Application{
    /**
     * Register the services here to make them general or register in the ModuleDefinition to make them module-specific
     */
    protected function _registerServices(){
        $di = new \Phalcon\DI\FactoryDefault();
        $loader = new \Phalcon\Loader();
        $loader->registerDirs(array(dirname(__DIR__).'/apps/library/'))->register();
        //Registering a router khai bao cac router
        $di->set('router', function(){
            $router = new \Phalcon\Mvc\Router(false);
            $router->setDefaultModule("frontend");
            $router->add('/', array(
                'module' => 'frontend',
                'controller' => 'post',
                'action' => 'homepage',
            ));
            
            $router->add('/{product:([a-z0-9_)(.-]+)}', array(
                'module' => 'frontend',
                'controller' => 'post',
                'action' => 'tagpage',
                'product'     => 1
            ));
            
            $router->add('/{product:([a-z0-9_)(.-]+)}.html', array(
                'module' => 'frontend',
                'controller' => 'post',
                'action' => 'detailpage',
                'product'     => 1
            ));
            
            $router->add('/dang-nhap', array(
                'module' => 'frontend',
                'controller' => 'login',
                'action' => 'index',
            ));
            
            $router->add('/dang-ky', array(
                'module' => 'frontend',
                'controller' => 'login',
                'action' => 'register',
            ));
            
            $router->add('/dang-xuat', array(
                'module' => 'frontend',
                'controller' => 'login',
                'action' => 'logout',
            ));
            
            
            $router->add('/quan-ly-bai-thi', array(
                'module' => 'frontend',
                'controller' => 'member',
                'action' => 'index',
            ));
            
            $router->add('/them-bai-thi', array(
                'module' => 'frontend',
                'controller' => 'member',
                'action' => 'add',
            ));
            
            $router->add('/bai-thi/id/{id:([0-9_)(.-]+)}', array(
                'module' => 'frontend',
                'controller' => 'member',
                'action' => 'edit',
                'id'     => 1
            ));
            
            $router->add('/xem-bai-thi/id/{id:([0-9_)(.-]+)}', array(
                'module' => 'frontend',
                'controller' => 'member',
                'action' => 'view',
                'id'     => 1
            ));                        
            
            $router->add('/thong-tin-tai-khoan', array(
                'module' => 'frontend',
                'controller' => 'member',
                'action' => 'profile',
                'id'     => 1
            ));  
            
            $router->add('/logout', array(
                'module' => 'frontend',
                'controller' => 'login',
                'action' => 'logout'
            )); 
            
            $router->add('/checkmail', array(
                'module' => 'frontend',
                'controller' => 'login',
                'action' => 'checkmail'
            ));  
            
            $router->add('/checkmaillogin', array(
                'module' => 'frontend',
                'controller' => 'login',
                'action' => 'checkmaillogin'
            ));  
            
            //
            //backend control pannel
            $router->add("/backend", array(
                'module' => 'backend',
                'controller' => 'login',
                'action' => 'index',
            ));
            $router->add("/backend/:controller/:action", array(
                'module' => 'backend',
                'controller' => 1,
                'action' => 2,
            ));
            $router->add("/backend/:controller/index/page/:int", array(
                'module' => 'backend',
                'controller' => 1,
                'action' => 'index',
                'page'   =>2
            ));
            $router->add("/backend/:controller/changeStatus/status/{id:[0-1]}/id/:int", array(
                'module' => 'backend',
                'controller' => 1,
                'action' => 'changeStatus',
                'status'   =>2,
                'cid'       =>3,
            ));
            $router->add("/backend/:controller/filter/status/{id:[0-1]}", array(
                'module' => 'backend',
                'controller' => 1,
                'action' => 'filter',
                'status'   =>2,
            ));
            $router->add("/backend/:controller/:action/id/:int", array(
                'module'      => 'backend',
                'controller'  => 1,
                'action'      => 2,
                'id'          => 3,
            ));
            $router->add("/scripts/ckeditor/ckfinder/ckfinder.html", array(
                'module'      => 'backend',
                'controller'  => 'admin-index',
                'action'      => 'ckfinder',
            ));
            $router->notFound(array(
                'module'      => 'backend',
                "controller" => "login",
                "action" => "route404"
            ));
            return $router;
        });
        	
      
	 	 $di->setShared('session', function () {
            $session = new SessionAdapter( array(
                'uniqueId' => 'daily_vegafone'
            ));
            $session->start();
            return $session;
        });
        $di->set('cookies', function() {
            $cookies = new \Phalcon\Http\Response\Cookies();
            $cookies->useEncryption(false);
            return $cookies;
        });
        $di->set('auth', function () {
            return new Auth();
        });
        $di->set('mail', function () {
            return new Mail();
        });

        $di->set('translate', function() {
            $messages = array();
            $language = 'vi';
            if (file_exists(APPLICATION_PATH."/apps/messages/".$language.".php")) {
                require APPLICATION_PATH."/apps/messages/".$language.".php";
            } else {
                require APPLICATION_PATH."/apps/messages/en.php";
            }
            return new \Phalcon\Translate\Adapter\NativeArray(array(
                "content" => $messages,
            ));
        });
        $di->set('phpMailer', function () {
            return new Send();
        });
        /**
         * Access Control List
         */
        $di->set('acl', function () {
            return new Acl();
        });
        $di->set('seo',function(){
            return new Seo();
        });
        

		
        $di->set('db', function () {
            return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
                "host" => "localhost",
                "username" => "root",
                "password" => "",
                "dbname" => "ph_vega_gameshow",
                "options" => array(
                     PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',PDO::ATTR_CASE => PDO::CASE_LOWER, PDO::ATTR_PERSISTENT => TRUE
                )
            ));
        });
        
        $di->set('viewCache', function() {
            //Cache data for one day by default
            $frontCache = new \Phalcon\Cache\Frontend\Data(array(
                "lifetime" => 86400
            ));
            $cache = new Phalcon\Cache\Backend\File($frontCache, array(
                "cacheDir" => APPLICATION_PATH."/apps/cache/files/"
            ));
            /*//Memcached connection settings
             $frontCache = new \Phalcon\Cache\Frontend\Output(array(
                "lifetime" => 86400
            ));
            $cache = new \Phalcon\Cache\Backend\Memcache($frontCache, array(
                "host" => "localhost",
                "port" => "11211"
            ));*/
            return $cache;
        });
        
        $this->setDI($di);
    }

    public function main() {
        $this->_registerServices();
        //Register the installed modules
        $this->registerModules(array(
            'frontend' => array(
                'className' => 'Apps\Frontend\Module',
                'path' => APPLICATION_PATH.'/apps/frontend/Module.php'
            ),
            'backend' => array(
                'className' => 'Apps\Backend\Module',
                'path' => APPLICATION_PATH.'/apps/backend/Module.php'
            )
        ));
       
        echo $this->handle()->getContent();
    }
}
$application = new Application();
$application->main();